package com.ruoyi.web.controller.system.indexweb;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.IndexUrl;
import com.ruoyi.system.service.IIndexUrlService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页轮播图Controller
 * 
 * @author ruoyi
 * @date 2021-07-24
 */
@Controller
@RequestMapping("/system/url")
public class IndexUrlController extends BaseController
{
    private String prefix = "system/url";

    @Autowired
    private IIndexUrlService indexUrlService;

    /**
     * 查询首页轮播图列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IndexUrl indexUrl)
    {
        startPage();
        List<IndexUrl> list = indexUrlService.selectIndexUrlList(indexUrl);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */
    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IndexUrl indexUrl)
    {
        List<IndexUrl> list = indexUrlService.selectIndexUrlList(indexUrl);
        ExcelUtil<IndexUrl> util = new ExcelUtil<IndexUrl>(IndexUrl.class);
        return util.exportExcel(list, "首页轮播图数据");
    }

    /**
     * 新增保存首页轮播图
     */
    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IndexUrl indexUrl)
    {
        indexUrl.setCreateTime(new Date(System.currentTimeMillis()));
        indexUrl.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(indexUrlService.insertIndexUrl(indexUrl));
    }

    /**
     * 修改保存首页轮播图
     */
    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IndexUrl indexUrl)
    {
        indexUrl.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(indexUrlService.updateIndexUrl(indexUrl));
    }

    /**
     * 修改状态
     */
    @PostMapping("/editState")
    @ResponseBody
    public AjaxResult editState(IndexUrl indexUrl)
    {
        indexUrl = indexUrlService.selectIndexUrlById(indexUrl.getId());
        if (!UtilTool.isNull(indexUrl)){
            int state = indexUrl.getState();
            if (state==0){
                indexUrl.setState(1);
            }else {
                indexUrl.setState(0);
            }
            indexUrl.setUpdateTime(new Date(System.currentTimeMillis()));
            return toAjax(indexUrlService.updateIndexUrl(indexUrl));
        }else {
            return AjaxResult.error("id所属数据不存在");
        }

    }

    /**
     * 删除首页轮播图
     */
    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(indexUrlService.deleteIndexUrlByIds(ids));
    }
}
